/**
 * Created by 邱鹏 on 2016/10/10.
 */
$(function () {

});
function showError(str) {
    $.toast(str, "forbidden", function() {
    });
}
function getValidCode() {

    var phone = $("#phoneTxt").val();

    phoneCheck(phone, function () {
        var urlStr = "/app/targetSale/sendValidCode";
        $.post(urlStr, {phone: phone}, function (response) {
            $("#validCodeInput").attr("placeholder", response.msg);
        }, "json");
    });
}

function phoneCheck(mobile, callback) {
    var flag = true;
    if (mobile.length == 0) {
        showError('请输入手机号码！');
        $("#phoneTxt").focus();
        flag = false;
    }
    if (mobile.length != 11) {
        showError('请输入有效的手机号码！');
        $("#phoneTxt").focus();
        flag = false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
        showError('请输入有效的手机号码！');
        $("#phoneTxt").focus();
        flag = false;
    }
    if (flag == true) {
        callback();
    }
}


function checkValidCode() {
    var phone = $("#phoneTxt").val();
    var nameStr = $("#nameInput").val();
    var validStr = $("#validCodeInput").val();
    var param = {phone: phone, name: nameStr, validCode: validStr};
    var urlStr = "/app/targetSale/checkValidCode";
    if(nameStr==""){
        showError('请输入姓名');
        return false;
    }else if(nameStr.length>10){
        showError('请输入正确的姓名');
        return false;
    }
    if(phone==""){
        showError('请输入手机号!');
        return false;
    }
    if(!$("#weuiAgree").is(':checked')){
        showError("请阅读并同意<相关条款>");
        return false;
    }

    $.post(urlStr, param, function (response) {
        if (response.code == "-1") {
            showError("验证码不正确");
        } else {
            window.location = "ok.html";
        }
    }, "json");

}

